package com.oedtech.esl.repository.sys;

import com.oedtech.esl.model.sys.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Repository
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    User findByUsername(String username);

    @Modifying
    @Transactional
    @Query(value = "delete from sys_user where id in(:ids)")
    int deleteUsersById(@Param("ids") List<Long> ids);

    @Query(value = "select source_flag from sys_user where username=?1", nativeQuery = true)
    Integer findSourceFlagByUsername(String username);

    @Query(value = "select id, source_flag sourceFlag,expire_time expireTime from sys_user where username=?1", nativeQuery = true)
    Map<String,Object> findUserByUsername(String username);
    @Modifying
    @Transactional
    @Query(value = "update sys_user set password=?1,modify_time=now(),modify_who=?3 where id=?2", nativeQuery = true)
    int changePassword(String password, Long id, String modifyWho);


    @Modifying
    @Transactional
    @Query(value = "update sys_user set modify_time=now(),modify_who=?1 where id=?2", nativeQuery = true)
    int relateUser(String modifyWho,Long id);
}
